<?php


header("Content-type: text/html; charset=utf-8");

function sendRequest($data, $url)
{
    $resp_data = array ();
    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-length:'.strlen($data)));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    $resp = curl_exec ( $ch );
    $resp_data = urldecode( $resp );
    curl_close($ch);
    return $resp_data;
};


$data_array = array();
$data_array['amount'] = '1';
$data_array['biz_type'] = 'WECHATPAY';
$data_array['order_id'] = "8006-20171228185751"; //'TEST20171130175453';
$data_array['call_back_url'] = "https:\/\/staging.quitesoondelivery.com:443\/mobile\/respond.php?code=ottpay";
$temp_data_array = $data_array;
ksort($temp_data_array);
$data_str = implode(array_values($temp_data_array));
$data_md5 = strtoupper(md5($data_str));
$user_key = '1F3C702C6CF3FE2B';
$aesKeyStr = strtoupper(substr(md5($data_md5.$user_key),8,16));
$data_json = json_encode($data_array);

//$aesKeyStr="43F07F952E063D9D";
//$data_json='{"order_id":"8006-20171228185751","amount":1,"biz_type":"WECHATPAY","call_back_url":"https:\/\/staging.quitesoondelivery.com:443\/mobile\/respond.php?code=ottpay"}';
$encrypted_data = Security::encrypt($data_json, $aesKeyStr);
echo "json_encode:".$data_json."<br/>";
echo "md5:".$data_md5."<br/>";
echo "aesKeyStr:".$aesKeyStr."<br/>";
echo "encrypted_data:".$encrypted_data."<br/>";

$params_array = array();
$params_array['action'] = 'ACTIVEPAY';
$params_array['version'] = '1.0';
$params_array['merchant_id'] = 'BC00002980';
$params_array['data'] = $encrypted_data;
$params_array['md5'] = $data_md5;
$params_json = json_encode($params_array, JSON_UNESCAPED_UNICODE);
echo $params_json."<br/>";
//$params_json='{"action":"ACTIVEPAY","version":"1.0","merchant_id":"BC00002980","data":"H6BnCqaS8E\/ISHcqzHaoKpp43GQszyGMmnY3UQlfDTV5I1piPgVbbc0WsdsVDyQuMA+1oNdWtO2BuSSJkZynf4Cu2tSxmCp2tyCsMCpQ5G7hk0rVHi3YMyWZxsmtbnOxlTmtWoWjbtjzY0iQIRKlKjy9pa+vgIi2rl+l66VKBHkCwKDr+ligwimU2zpV7pc2xJ\/j6TRKB0qNNIcA5kDjhKHQvQ6irGqRyRfxLTvXAGc=","md5":"79C66B9ED3C35A60DFB041DF5DE741D1"}';
$resp_data = sendRequest($params_json, 'https://frontapi.ottpay.com:443/process');
var_dump($resp_data);
$resp_arr = (array) json_decode($resp_data, true);
$aesKeyStr = strtoupper(substr(md5($resp_arr['md5'].$user_key),8,16));
$decrypted_data = Security::decrypt($resp_arr['data'], $aesKeyStr);

$return_data_arr = (array) json_decode($decrypted_data, true);
$qrCode_url = $return_data_arr['code_url'];
var_dump($return_data_arr);
//echo 'response qrCode_url = '.$qrCode_url;


class Security
{
    public static function encrypt($input, $key)
    {
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $input = Security::pkcs5_pad($input, $size);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $key, $iv);
        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
//        $data = utf8_encode(base64_encode($data));
        $data = base64_encode($data);
        return $data;
    }

	
	    private static function pkcs5_pad ($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    public static function decrypt($sStr, $sKey)
    {
        $decrypted= mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $sKey, base64_decode(str_replace(" ","+",$sStr)), MCRYPT_MODE_ECB);
        $dec_s = strlen($decrypted);
        $padding = ord($decrypted[$dec_s-1]);
        $decrypted = substr($decrypted, 0, -$padding);
        return $decrypted;
    }
}
